package es.ats.codington.spring.controllers;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;



import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.LargeBusiness;
import es.ats.codington.festival.domain.Museum;
import es.ats.codington.festival.domain.Park;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.domain.Stadium;
import es.ats.codington.festival.domain.Theater;
import es.ats.codington.festival.domain.TouristAtracction;
import es.ats.codington.festival.domain.TraditionalMarkets;
import es.ats.codington.festival.domain.Zoo;
import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.services.PlaceService;



@Controller
public class PlaceController {
	public enum TypeName{Stadium, LargeBusiness, Theater, Museum, TraditionalMarkets,Zoo,TouristatrAcction,Park}
	


@RequestMapping(value = "/home.htm")
public String showhome(HttpServletRequest request, HttpServletResponse response) throws Exception {
	return "mainpage2";
}

@RequestMapping(value = "/history.htm")
public String showhistory(HttpServletRequest request, HttpServletResponse response) throws Exception {
	return "history";
}

@RequestMapping(value = "/showplace.htm")
public ModelAndView showplaceController(HttpServletRequest request, HttpServletResponse response) throws Exception {
	ModelAndView modelAndView = new ModelAndView();		

	
	
	PlaceService ps = new PlaceService();
	
	try{
		int idplace = Integer.parseInt(request.getParameter("idplace"));
		Place place = ps.viewPlace(idplace);
		
		modelAndView.addObject("place",place);
		modelAndView.setViewName("viewplace");

										
	   
	   
		}catch(Exception ex){
			ex.getMessage();
			
			
		}
	return modelAndView;
	 
	
	 
}



@RequestMapping(value = "/listzoo.htm")
public ModelAndView listzooController(HttpServletRequest request, HttpServletResponse response) throws Exception {
	ModelAndView modelAndView = new ModelAndView();		
	PlaceService placeService = new PlaceService();


	PlaceService ps = new PlaceService();
	ArrayList<Integer> idsplace = new ArrayList<Integer>();
	ArrayList<Place> allplaces = new ArrayList<Place>();
	int iter;
	int size;
	
	try{
		String type = (String) request.getParameter("type");
		
		idsplace= ps.getIdsPlace(type);
		size= idsplace.size();
		
		for (iter=0; iter<size; iter++){
			int id= idsplace.get(iter);
			Place place = ps.viewPlace(id);
			allplaces.add(place);
		}
				
		int siz= allplaces.size();
		modelAndView.addObject("size", siz);
		modelAndView.setViewName("viewplace");
		modelAndView.addObject("places", allplaces);
		
		modelAndView.setViewName("/EventsByPlaceSouth"); 
		 
	
	}catch(Exception ex){
		ex.getMessage();
		
	}
	return modelAndView;
}
@RequestMapping(value = "/listpark.htm")
public ModelAndView listparkController(HttpServletRequest request, HttpServletResponse response) throws Exception {
	ModelAndView modelAndView = new ModelAndView();		
	PlaceService placeService = new PlaceService();


	PlaceService ps = new PlaceService();
	ArrayList<Integer> idsplace = new ArrayList<Integer>();
	ArrayList<Place> allplaces = new ArrayList<Place>();
	int iter;
	int size;
	
	try{
		String type = (String) request.getParameter("type");
		
		idsplace= ps.getIdsPlace(type);
		size= idsplace.size();
		
		for (iter=0; iter<size; iter++){
			int id= idsplace.get(iter);
			Place place = ps.viewPlace(id);
			allplaces.add(place);
		}
				
		int siz= allplaces.size();
		modelAndView.addObject("size", siz);
		modelAndView.setViewName("viewplace");
		modelAndView.addObject("places", allplaces);
		
		modelAndView.setViewName("/EventsByPlaceSouth"); 
		 
	
	}catch(Exception ex){
		ex.getMessage();
		
	}
	return modelAndView;
}
@RequestMapping(value = "/listattraction.htm")
public ModelAndView listattractionController(HttpServletRequest request, HttpServletResponse response) throws Exception {
	ModelAndView modelAndView = new ModelAndView();		
	PlaceService placeService = new PlaceService();


	PlaceService ps = new PlaceService();
	ArrayList<Integer> idsplace = new ArrayList<Integer>();
	ArrayList<Place> allplaces = new ArrayList<Place>();
	int iter;
	int size;
	
	try{
		String type = (String) request.getParameter("type");
		
		idsplace= ps.getIdsPlace(type);
		size= idsplace.size();
		
		for (iter=0; iter<size; iter++){
			int id= idsplace.get(iter);
			Place place = ps.viewPlace(id);
			allplaces.add(place);
		}
				
		int siz= allplaces.size();
		modelAndView.addObject("size", siz);
		modelAndView.setViewName("viewplace");
		modelAndView.addObject("places", allplaces);
		
		modelAndView.setViewName("/EventsByPlaceSouth"); 
		 
	
	}catch(Exception ex){
		ex.getMessage();
		
	}
	return modelAndView;
}
@RequestMapping(value = "/listmarket.htm")
public ModelAndView listmarketController(HttpServletRequest request, HttpServletResponse response) throws Exception {
	ModelAndView modelAndView = new ModelAndView();		
	PlaceService placeService = new PlaceService();


	PlaceService ps = new PlaceService();
	ArrayList<Integer> idsplace = new ArrayList<Integer>();
	ArrayList<Place> allplaces = new ArrayList<Place>();
	int iter;
	int size;
	
	try{
		String type = (String) request.getParameter("type");
		
		idsplace= ps.getIdsPlace(type);
		size= idsplace.size();
		
		for (iter=0; iter<size; iter++){
			int id= idsplace.get(iter);
			Place place = ps.viewPlace(id);
			allplaces.add(place);
		}
				
		int siz= allplaces.size();
		modelAndView.addObject("size", siz);
		modelAndView.setViewName("viewplace");
		modelAndView.addObject("places", allplaces);
		
		modelAndView.setViewName("/EventsByPlaceSouth"); 
		 
	
	}catch(Exception ex){
		ex.getMessage();
		
	}
	return modelAndView;
}
@RequestMapping(value = "/listmuseum.htm")
public ModelAndView listmuseumController(HttpServletRequest request, HttpServletResponse response) throws Exception {
	ModelAndView modelAndView = new ModelAndView();		
	PlaceService placeService = new PlaceService();


	PlaceService ps = new PlaceService();
	ArrayList<Integer> idsplace = new ArrayList<Integer>();
	ArrayList<Place> allplaces = new ArrayList<Place>();
	int iter;
	int size;
	
	try{
		String type = (String) request.getParameter("type");
		
		idsplace= ps.getIdsPlace(type);
		size= idsplace.size();
		
		for (iter=0; iter<size; iter++){
			int id= idsplace.get(iter);
			Place place = ps.viewPlace(id);
			allplaces.add(place);
		}
				
		int siz= allplaces.size();
		modelAndView.addObject("size", siz);
		modelAndView.setViewName("viewplace");
		modelAndView.addObject("places", allplaces);
		
		modelAndView.setViewName("/EventsByPlace"); 
		 
	
	}catch(Exception ex){
		ex.getMessage();
		
	}
	return modelAndView;
}
@RequestMapping(value = "/listtheater.htm")
public ModelAndView listtheaterController(HttpServletRequest request, HttpServletResponse response) throws Exception {
	ModelAndView modelAndView = new ModelAndView();		
	PlaceService placeService = new PlaceService();


	PlaceService ps = new PlaceService();
	ArrayList<Integer> idsplace = new ArrayList<Integer>();
	ArrayList<Place> allplaces = new ArrayList<Place>();
	int iter;
	int size;
	
	try{
		String type = (String) request.getParameter("type");
		
		idsplace= ps.getIdsPlace(type);
		size= idsplace.size();
		
		for (iter=0; iter<size; iter++){
			int id= idsplace.get(iter);
			Place place = ps.viewPlace(id);
			allplaces.add(place);
		}
				
		int siz= allplaces.size();
		modelAndView.addObject("size", siz);
		modelAndView.setViewName("viewplace");
		modelAndView.addObject("places", allplaces);
		
		modelAndView.setViewName("/EventsByPlace"); 
		 
	
	}catch(Exception ex){
		ex.getMessage();
		
	}
	return modelAndView;
}
@RequestMapping(value = "/liststadium.htm")
public ModelAndView liststadiumController(HttpServletRequest request, HttpServletResponse response) throws Exception {
	ModelAndView modelAndView = new ModelAndView();		
	PlaceService placeService = new PlaceService();


	PlaceService ps = new PlaceService();
	ArrayList<Integer> idsplace = new ArrayList<Integer>();
	ArrayList<Place> allplaces = new ArrayList<Place>();
	int iter;
	int size;
	
	try{
		String type = (String) request.getParameter("type");
		
		idsplace= ps.getIdsPlace(type);
		size= idsplace.size();
		
		for (iter=0; iter<size; iter++){
			int id= idsplace.get(iter);
			Place place = ps.viewPlace(id);
			allplaces.add(place);
		}
				
		int siz= allplaces.size();
		modelAndView.addObject("size", siz);
		modelAndView.setViewName("viewplace");
		modelAndView.addObject("places", allplaces);
		
		modelAndView.setViewName("/EventsByPlace"); 
		 
	
	}catch(Exception ex){
		ex.getMessage();
		
	}
	return modelAndView;
}
@RequestMapping(value = "/listbusiness.htm")
public ModelAndView listbysubessController(HttpServletRequest request, HttpServletResponse response) throws Exception {
	ModelAndView modelAndView = new ModelAndView();		
	PlaceService placeService = new PlaceService();


	PlaceService ps = new PlaceService();
	ArrayList<Integer> idsplace = new ArrayList<Integer>();
	ArrayList<Place> allplaces = new ArrayList<Place>();
	int iter;
	int size;
	
	try{
		String type = (String) request.getParameter("type");
		
		idsplace= ps.getIdsPlace(type);
		size= idsplace.size();
		
		for (iter=0; iter<size; iter++){
			int id= idsplace.get(iter);
			Place place = ps.viewPlace(id);
			allplaces.add(place);
		}
				
		int siz= allplaces.size();
		modelAndView.addObject("size", siz);
		modelAndView.setViewName("viewplace");
		modelAndView.addObject("places", allplaces);
		
		modelAndView.setViewName("/EventsByPlace"); 
		 
	
	}catch(Exception ex){
		ex.getMessage();
		
	}
	return modelAndView;
}

@RequestMapping(value= "/pre_createplace.htm")
public String preCreateplace(HttpServletRequest request, HttpServletResponse response) throws IOException {
	return "createplace";
}

@RequestMapping(value= "/post_createplace.htm")
public String postCreateplace(HttpServletRequest request, HttpServletResponse response) throws IOException {
	
		String nameplace=request.getParameter("nameplace");
		String description=request.getParameter("descriptions");
		String address=request.getParameter("address");
		int capacity=Integer.parseInt(request.getParameter("capacity"));
		String type=request.getParameter("type");

		try{
			boolean tt = false;
			PlaceService ps = new PlaceService();
			
			switch(TypeName.valueOf(type)){
			
			case Stadium:  
				
				Stadium stadium=new Stadium();
				stadium.setName(nameplace);
				stadium.setAddress(address);
				stadium.setDescription(description);
				stadium.setCapacity(capacity);
				stadium.setTown("North");
				stadium.setType(type);
				tt = ps.createPlace(stadium);
				

				break;
				
			case LargeBusiness:
				
				LargeBusiness largebusiness = new LargeBusiness();
				largebusiness.setName(nameplace);
				largebusiness.setAddress(address);
				largebusiness.setDescription(description);
				largebusiness.setCapacity(capacity);
				largebusiness.setTown("North");
				largebusiness.setType(type);
				tt = ps.createPlace(largebusiness);
				
				
				break;
				
			case Theater:
				
				Theater theater = new Theater();
				theater.setName(nameplace);
				theater.setAddress(address);
				theater.setDescription(description);
				theater.setCapacity(capacity);
				theater.setTown("North");
				theater.setType(type);
				tt = ps.createPlace(theater);
				
				
				break;
				
			case  Museum:
				
				Museum museum = new Museum();
				museum.setName(nameplace);
				museum.setAddress(address);
				museum.setDescription(description);
				museum.setCapacity(capacity);
				museum.setTown("North");
				museum.setType(type);
				tt = ps.createPlace(museum);
				
				
				break;
				
			case TraditionalMarkets:
				
				TraditionalMarkets traditionalmarkets = new TraditionalMarkets();
				traditionalmarkets.setName(nameplace);
				traditionalmarkets.setAddress(address);
				traditionalmarkets.setDescription(description);
				traditionalmarkets.setCapacity(capacity);
				traditionalmarkets.setTown("South");
				traditionalmarkets.setType(type);
				tt = ps.createPlace(traditionalmarkets);
				
				
				break;
				
			case Zoo:
				
				Zoo zoo = new Zoo();
				zoo.setName(nameplace);
				zoo.setAddress(address);
				zoo.setDescription(description);
				zoo.setCapacity(capacity);
				zoo.setTown("South");
				zoo.setType(type);
				tt = ps.createPlace(zoo);
				
				
				break;
				
			case TouristatrAcction:
				
				TouristAtracction touristatracction = new TouristAtracction();
				touristatracction.setName(nameplace);
				touristatracction.setAddress(address);
				touristatracction.setDescription(description);
				touristatracction.setCapacity(capacity);
				touristatracction.setTown("South");
				touristatracction.setType(type);
				tt = ps.createPlace(touristatracction);
				
				
				break;
				
			case Park:
				
				Park park = new Park();
				park.setName(nameplace);
				park.setAddress(address);
				park.setDescription(description);
				park.setCapacity(capacity);
				park.setTown("South");
				park.setType(type);
				tt = ps.createPlace(park);
				
				
				break;
					
			default: 
				
					break;
			}
				       
 		    if (tt==true){	
 		    	return "mainpageadmin";
 		    
 		    }else{
 		    	return "createplace"; 
 		         }
 	    }catch(Exception ex){
 			ex.getMessage();
 			return "mainpageadmin";
 	    }
	
}
}



